

*****************************************************************************************************************************************
*** The following code has been modified in order to add the dummy that takes value 1 if the entrepreneur if female and 0 otherwise.
******************************************************************************************************************************************

/*
*******************************************************************************************************************************************************
* Program Purpose: 
- starts with the business panel of 5 survey waves
- creates analysis variables at the business level

*******************************************************************************************************************************************************
*/

	clear
	use "$data_inter/Panel Business Level.dta"

***********************************************************************************************************************************	
*
**STEP 0- MAKING SOME IMPORTANT/ADMIN VARIABLES AT THE HOUSEHOLD LEVEL
*
***********************************************************************************************************************************
	
	************************************
	**CLIENT ACROSS ALL ROUNDS
	************************************
		***********
		*THESE PEOPLE WERE NOT SURVEYED AT BASELIN- WERE NOT IN THE ROSTER BUT WERE IN THE HH
		***********
		recode client (.=0) if Business_Owner_Id==1957|Business_Owner_Id==2115
		
	bys Id Business_Owner_Id: egen Client=max(client)
		
	************************************
	**LOTTERY WINNER
	************************************	
	*identifies the round at which the lottery is won
	gen Winner=LotteryWinner
	recode Winner (1=0) if Survey_Version==1
	*identifies the hh as a lottery winner
	gen Winner2=LotteryWinner
	
	***********
	*SURVEY DATE/MONTH
	***********

	bys Id Survey_Version: egen survey_date=max(enddate)
		drop enddate
		
	*SURVEY MONTH
	gen survey_month=month(survey_date) 

	***********
	*SURVEYOR CODE
	***********		
	rename Surveyor_Code Surveyor_Code2
	egen surveyor_code=group(Surveyor_Code2)
	bys Id Survey_Version: egen Surveyor_Code=max(surveyor_code)
		
***********************************************************************************************************************************	
*
**STEP 1- BASELINE VARIABLES
*For people who did not have a particular businesses at baseline, merging in their baseline characteristics
*
***********************************************************************************************************************************

	preserve
	
		use "$data_raw\survey rounds\Survey Round 1.dta", clear
		
		keep if Business_Owner_Id!=.
		
		duplicates drop Id Business_Owner_Id, force
		
		keep Id Business_Owner_Id hhroster_2- child_6to12 TV_hhincome_1- digitspan_1
		
		sort Id Business_Owner_Id  
		
		tempfile temp
		
		save `temp'
		
	restore
	
	sort Id Business_Owner_Id  
	
	merge m:1  Id Business_Owner_Id   using `temp'	, update	
	
		***********
		*THESE PEOPLE WERE NOT SURVEYED AT BASELIN- WERE NOT IN THE ROSTER BUT WERE IN THE HH
		***********
		assert _merge==1 if (Business_Owner_Id==1957|Business_Owner_Id==2115)
		
		assert _merge!=2 | _merge!=5
		
		drop _merge

***********************************************************************************************************************************	
*
**STEP 1- BASELINE SURVEY VARIABLE CREATION 
*Need the full baseline survey not just the businesses
*
***********************************************************************************************************************************
	{

	preserve
	
		use "$data_raw\survey rounds\Survey Round 1.dta", clear

		************************************
		**One response per hh member
		************************************	
		*to account for the fact that the same data is duplicated if the person has multiple businesses
		egen tag_HH_Member_Id=tag(HH_Member_Id)
		
		************************************
		**HOUSEHOLD OCCUPATION COMPOSITION
		************************************	
		recode hhroster_10 hhroster_12 hhroster_13(-999 -777=0) (2=0)

		local variables hhroster_10 hhroster_12 hhroster_13
		local names Number_Fixed_Salary Number_Daily_Wage Number_Self_Employed
		
		forvalues i=1/3{
			local var: word `i' of `variables'
			local name: word `i' of `names'
			
				tempvar temp`i' 
					gen `temp`i'' = `var' if tag_HH_Member_Id
				bys Id: egen `name'=sum(`temp`i'')
				
		}
		
		bys Id: egen Total_Nu_Bus=sum(businesslist_5)

			
		label var Number_Fixed_Salary "Nu Fixed Salary Workers in HH at Baseline"
		label var Number_Daily_Wage "Nu Fixed Salary Workers in HH at Baseline"
		label var Number_Self_Employed "Nu Fixed Salary Workers in HH at Baseline"
		label var Total_Nu_Bus "Total Nu of Open Businesses in HH at Baseline"
		
		*******************************************************
		*KEEPING ONLY OPEN BUSINESSES
		*******************************************************	
		
		keep if businesslist_5==1
		
		***********************************************************************************************************************************
		**TOTAL ASSETS
		***********************************************************************************************************************************

		*CREATING TOTAL BUSINESS ASSETS
		*Recoding
		recode Asset*Value* (. -777=0)
		recode Asset*Value* (-666 -998 -999=.)

		*Creating Variables for each Asset type
		egen Business_Tools=rowtotal(Asset*Tool*Value*),m
		egen Business_Furniture=rowtotal(Asset*Furniture*Value*),m
		egen Business_Livestock=rowtotal(Asset*Livestock*Value*),m
		egen Business_Machine=rowtotal(Asset*Machine*Value*),m
		egen Business_Vehicle=rowtotal(Asset*Vehicle*Value*),m
		egen Business_Land=rowtotal(Asset*Land*Value*),m

		*Aggregating
		egen Business_Ass=rowtotal(Business_Tools Business_Furniture Business_Livestock ///
		Business_Machine Business_Vehicle Business_Land),m
		bys Id: egen Business_Assets=sum(Business_Ass)
			drop Business_Ass
			***************
			*SEPARATING HOUSEHOLD AND BUSINESS ASSETS TO MAKE SURE THEY ARE NOT DOUBLE COUNTED
			***************

			gen Asset_Car=TV_hhasset6_2 if Asset_Vehicle_Value_1!=TV_hhasset6_2 & TV_hhasset6_1!=0
				replace Asset_Car=. if Id==106406

			gen Asset_Auto=TV_hhasset3_2 if Asset_Vehicle_Value_1!=TV_hhasset3_2 & TV_hhasset3_1!=0
				replace Asset_Auto=. if Id==104362

			gen Asset_Rickshaw=TV_hhasset4_2 if Asset_Vehicle_Value_1==0 & TV_hhasset4_1!=0

			gen Bike=1 if regexm(Asset_Vehicle_Name_1, "BIKE")|regexm(Asset_Vehicle_Name_1, "TWO")| ///
				regexm(Asset_Vehicle_Name_1, "2 VILAR")|regexm(Asset_Vehicle_Name_1, "ACTIVA")| ///
				regexm(Asset_Vehicle_Name_1, "BAI")|regexm(Asset_Vehicle_Name_1, "BICKY")|  ///  
				regexm(Asset_Vehicle_Name_1, "MOTER")|regexm(Asset_Vehicle_Name_1, "MOTAR")| ///
				regexm(Asset_Vehicle_Name_1, "SCOO")|regexm(Asset_Vehicle_Name_1, "SPENDOR") | ///
				regexm(Asset_Vehicle_Name_1, "TOO")|regexm(Asset_Vehicle_Name_1, "SPLENDOR") | ///
				regexm(Asset_Vehicle_Name_1, "MOTOR")

			gen Asset_MotorBike=TV_hhasset5_2 if Bike==. & TV_hhasset5_1!=0
				replace Asset_MotorBike=TV_hhasset5_2 if  TV_hhasset5_1>=2

			gen Asset_Land=TV_hhasset19_2 if Asset_Land_Name_1!="LAND"
				replace Asset_Land=. if Id==113132

			recode Asset_Car Asset_Auto Asset_Rickshaw Asset_MotorBike Asset_Land (-555 -999=.)

			egen Asset_Vehicle=rowtotal(Asset_Car Asset_Auto Asset_Rickshaw Asset_MotorBike),m
				recode Asset_Vehicle  Asset_Land (.=0)
			 
			*Recoding HOUSEHOLD ASSETS
			recode TV_hhasset*_2 ( -777 . =0)
			recode TV_hhasset*_2 (-999 -555  -666   -998 =.)

			*Excluding the double counting from business (These are Household Assets)
			egen HH_Assets=rowtotal(TV_hhasset1_2 TV_hhasset7_2 TV_hhasset8_2 TV_hhasset9_2 ///
			TV_hhasset10_2 TV_hhasset11_2 TV_hhasset12_2 TV_hhasset13_2 TV_hhasset15_2 ///
			TV_hhasset16_2 TV_hhasset17_2 TV_hhasset18_2 Asset_Vehicle Asset_Land),m

			egen Total_Assets=rowtotal(HH_Assets  Business_Assets),m

		label var HH_Assets "Value HH Assets at Baseline"
		label var Business_Assets "Value Business Assets at Baseline"

		
		****************************
		*AVG Yearly Profits
		****************************
		recode seasonality_1* (-555 -666 -999 -998=.) (-777=0)
		egen float Avg_Yrly_Profits=rowmean(seasonality_1*)
		label var  Avg_Yrly_Profits "Average Monthly Profits in Past Year"
		
		****************************
		*KEEPING THE NEWLY CREATED VARIABLES
		****************************
		gen Survey_Version=1 
		
		keep Id Business_Owner_Id Business_Name  Number_Fixed_Salary- Survey_Version
		
		sort Id Business_Owner_Id Business_Name Survey_Version
		tempfile temp
		save `temp'
		
	restore
	
	sort Id Business_Owner_Id Business_Name Survey_Version
	merge  Id Business_Owner_Id Business_Name Survey_Version using `temp'
	tab _merge
	drop if _merge==2
	drop _merge

	}
	
	***********************************************************************************************************************************	
	*
	** STEP 2- BUSINESS TYPE CLASSIFICATION
	*
	***********************************************************************************************************************************
	**********************************************
	*MERGING IN MANUAL BUSINESS CLASSIFICATION DONE BY RA
	**********************************************
	sort  Id Business_Owner_Id Business_Name 
		merge  m:1 Id Business_Owner_Id Business_Name  using "$data_raw\survey rounds\Business Classifications.dta"
		drop _merge
		
	**********************************************
	*CREATING SECTOR DUMMIES FOR ALL BUSINESSES
	**********************************************
	gen manufacturing=businessstatus_1_new==1 | businessstatus_1_new==2 
	
	gen retail=businessstatus_1_new==4
	
	gen service=businessstatus_1_new==3 | businessstatus_1_new==5| ///
	businessstatus_1_new==6 | businessstatus_1_new==7 | ///
	businessstatus_1_new==10 | businessstatus_1_new==11
	
	gen agriculture=businessstatus_1_new==9

	**********************************************
	*CREATING SECTOR DUMMIES FOR THE CLIENT'S MAIN BUSINESS
	*main business is defined as most profitable in yrly avg
	**********************************************s
	/*
	select the main baseline business of the client 
	(sometimes client has 2 or 3 businesses)
	below we also break ties if two businesses have the same profs
	*/
	
	bys Id Business_Owner_Id Survey_Version: egen sum_client=sum(Client) if Survey_Version==1

	
	gen max_prof_baseline=Avg_Yrly_Profits if Survey_Version==1 & ///
		(sum_client==2|sum_client==3)
		
	bys Id Business_Owner_Id: egen float max_prof_business=max(Avg_Yrly_Profits) ///
		if Survey_Version==1  & (sum_client==2|sum_client==3)
		*same avgs yearly profits, just broke the indifference
		recode max_prof_business (0=.) if Id==109076 & Business_Name=="TAILORING"
		recode max_prof_business (9333.333=.) if Id==106190 & Business_Name=="TRANSPORT"
	
	************************************
	*Detailed Business sector for the client
	***********************************
	gen client_bus_sector_det=Business_Sector_new if  sum_client==1
		replace client_bus_sector_det=Business_Sector_new if ///
			client_bus_sector_det==. &  (sum_client==2|sum_client==3) ///
			&  max_prof_business==Avg_Yrly_Profits

	************************************
	*Broad business sector category for the client
	***********************************
	gen client_bus_sector_broad=businessstatus_1_new if  sum_client==1
		replace client_bus_sector_broad=businessstatus_1_new if ///
			client_bus_sector_broad==. &  (sum_client==2|sum_client==3) ///
			&  max_prof_business==Avg_Yrly_Profits
			

	************************************
	*Broad business sector category for the client
	*manufacturing retail service
	***********************************			
	gen B_manufacturing=(client_bus_sector_broad==1 | client_bus_sector_broad==2 ) 	///
		if Survey_Version==1 & client==1
	
	gen B_retail=(client_bus_sector_broad==4 )   if Survey_Version==1  & client==1
	
	gen B_service=(client_bus_sector_broad==3 | client_bus_sector_broad==5 | ///
		client_bus_sector_broad==6 | client_bus_sector_broad==7 | ///
		client_bus_sector_broad==10 | client_bus_sector_broad==11)  ///
		if Survey_Version==1  & client==1
	
	gen B_agriculture=(client_bus_sector_broad==9 ) if  ///
		 Survey_Version==1 & client==1
	
	*recodes to missing the client businesses that aren't the primary
	egen allbusiness=rowtotal(B_manufacturing B_retail B_service B_agriculture),m
		recode B_manufacturing B_retail B_service B_agriculture (0=.) if allbusiness==0
	
	*creates a household level variable for all the businesses 
	local i=1
	foreach var of varlist B_manufacturing B_retail B_service B_agriculture client_bus_sector_det client_bus_sector_broad{
			tempvar temp`i'
			bys Id: egen `temp`i''=max(`var')
			replace `var'=`temp`i''
		local i=`i'+1
	}
	
			drop max_prof_baseline max_prof_business 

	
	***********************************************************************************************************************************	
	*
	** STEP 3- OTHER VARIABLES
	*
	***********************************************************************************************************************************

	
	********************************
	*HOUSEHOLD INCOME
	********************************
	
	gen Income=hhroster_21 if Survey_Version==1 	
	replace Income=credit1 if Survey_Version==2
	replace Income=q18 if Survey_Version==3| Survey_Version==4|Survey_Version==5
	
	*******************************************************************************************************************************************************
	*SELF-REPORTED MR	
	*******************************************************************************************************************************************************
	gen Marginal_Return=TV_marginal2-TV_marginal1
	tempvar temporary
		bys Id: egen `temporary'=max(Marginal_Return)
		replace Marginal_Return=`temporary'
		

	tempvar temporary
		bys Id: egen `temporary'=max(TV_marginal2)
		replace TV_marginal2=`temporary'
		
	*******************************************************************************************************************************************************
	*MEDICAL EXPENSES
	*******************************************************************************************************************************************************
	gen Med_Exp_30Days=TV_Medical_1 if Survey_Version==1
		replace Med_Exp_30Days=credit6 if  Survey_Version==2
		replace Med_Exp_30Days=q21 if Survey_Version==3| Survey_Version==4|Survey_Version==5
		

	*******************************************************************************************************************************************************
	*GROUP SIZE
	*******************************************************************************************************************************************************
	tempvar temp
	by GroupNumber Id, sort: gen `temp' = _n == 1 
        by GroupNumber:egen Group_Size = sum(`temp')
		
	cap drop __*
		
***********************************************************************************************************************************	
*
** STEP 3- BUSINESS CHARACTERISTICS
*
***********************************************************************************************************************************

***********************************************************************************************************************************
**REVENUES, PROFITS
***********************************************************************************************************************************


	********************************
	*RECODING BUSINESS LEVEL OUTCOMES 
	********************************
	
	recode RMS_1  rentals_2 Piecerate_1 LivestockFish_2 LivestockFish_25 LivestockFish_32 LivestockFish_33 LivestockFish_38 Construction_10 Construction_11(-555 -666 -999 -998=.) (-777=0)
	recode RMS_24 Farming_16 RMS_28 Piecerate_19 Piecerate_21 LivestockFish_25 LivestockFish_29 LivestockFish_33 LivestockFish_38 Construction_12 Construction_13   Commission_4 Commission_7 rentals_2(-555 -666 -999 -998=.) (-777=0)
	recode RMS_3 RMS_4 RMS_7 Piecerate_3 LivestockFish_4 LivestockFish_5 LivestockFish_6 LivestockFish_34 Construction_14 (-555 -666 -999 -998=.) (-777=0)
	recode RMS_9 RMS_10 Piecerate_5 LivestockFish_8 LivestockFish_10(-555 -666 -999 -998=.) (-777=0)

		

	********************************
	*CONSTRUCTION AND FISHING/AG HAVE SUBPROJECTS WE HAVE TO AGGREGATE
	********************************	
	*LIVESTOCK AND CONSTRUCTION HAVE SUBPROJECTS-
	*HAVE TO AGGREGATE AT THE SUBID LEVEL BEFORE ADDING

		*Revenues for Livestock byproduct
		bys Id Business_Owner_Id Business_Name Survey_Version: egen Total_LivestockFish_32=sum(LivestockFish_32)
		*Profits for Livestock byproduct
		bys Id Business_Owner_Id Business_Name  Survey_Version: egen Total_LivestockFish_33=sum(LivestockFish_33)
		*Avg Weekly Profits for Livestock byproduct
		bys Id Business_Owner_Id Business_Name  Survey_Version: egen Total_LivestockFish_38=sum(LivestockFish_38)
		
		recode Total_LivestockFish_32 Total_LivestockFish_38  Total_LivestockFish_33 (0=.) if LivestockFish_1==.

		egen LivestockFish_Revenues=rowtotal(Total_LivestockFish_32 LivestockFish_2),m
		egen LivestockFish_Profits=rowtotal(Total_LivestockFish_33 LivestockFish_25),m
		egen LivestockFish_WeekProfits=rowtotal(Total_LivestockFish_38 LivestockFish_29),m

		*Construction
		bys Id Business_Owner_Id Business_Name Survey_Version: egen Total_Construction_10=sum( Construction_10)
		bys Id Business_Owner_Id Business_Name Survey_Version: egen Total_Construction_11=sum( Construction_11)

		egen Construction_Rev=rowtotal(Total_Construction_10 Total_Construction_11 Construction_4),m

		gen Construction_Revenues=Construction_Rev*30 if Construction_1==1
		replace Construction_Revenues=Construction_Rev*4 if Construction_1==2
		replace Construction_Revenues=Construction_Rev if Construction_1==3
		replace Construction_Revenues=Construction_Rev/6 if Construction_1==4
		replace Construction_Revenues=Construction_Rev/12 if Construction_1==5


	*COmmission- creating a monthly variable
	gen Commission_Revenues=Commission_3*30 if Commission_1==1
		replace Commission_Revenues=Commission_3*4 if Commission_1==2
		replace Commission_Revenues=Commission_3 if Commission_1==3
		replace Commission_Revenues=Commission_3/6 if Commission_1==4
		replace Commission_Revenues=Commission_3/12 if Commission_1==5
	
	*Farming- creating a monthly variable
	gen Farming_Revenues=Farming_2 /12
	
	*********
	*CREATING REVENUES AND PROFITS VARIABLES
	**********
	egen Revenues_30Days=rowtotal(RMS_1   Piecerate_1 LivestockFish_Revenues  Construction_Revenues Commission_Revenues Farming_Revenues  ), m
	egen Profits_30Days=rowtotal(RMS_24 Piecerate_19 LivestockFish_Profits Construction_12 Commission_4 Farming_16 ),m

	*For Survey_Versions>=3 - the name of the variable (not the question) changed in later surveys
	replace   Revenues_30Days=q9 if  Survey_Version==3|Survey_Version==4|Survey_Version==5
	replace  Profits_30Days= q10 if  Survey_Version==3|Survey_Version==4|Survey_Version==5

	*Recoding profits and revenues to be 0 of business is closed
	recode Profits_30Days Revenues_30Days (.=0) if Business_Open==0
		

	********************************
	*INVENTORY
	********************************	
	egen Inventory=rowtotal(RMS_3 RMS_4 RMS_7 Piecerate_3 LivestockFish_4 LivestockFish_5 LivestockFish_6 LivestockFish_34 Construction_14 )
	replace Inventory=q12 if Survey_Version>=3
	egen Total_Spent_Inventory_30Days=rowtotal(RMS_9 RMS_10 Piecerate_5 LivestockFish_8 LivestockFish_10),m


	drop Total_LivestockFish_32- Commission_Revenues
	
	************************************
	**BUSINESS ASSETS AND INVENTORY FOR ALL ROUNDS
	************************************
	egen Total_Business_Assets=rowtotal( q14_4p*  q15_4p*)
	replace Total_Business_Assets=Business_Assets if Survey_Version==1	
	replace Total_Business_Assets=Value_Assets if Survey_Version==2	
	
	sort Id Business_Owner_Id Business_Name Survey_Version
	
	*After the first 2 rounds, just asked for changes in assets
		tempvar temp
			sort Id Business_Owner_Id Business_Name Survey_Version
			by Id : gen `temp'=Total_Business_Assets[_n-1]  
			replace Total_Business_Assets= `temp'+Total_Business_Assets  if Survey_Version==2
			
		tempvar temp
			sort Id Business_Owner_Id Business_Name Survey_Version
			by Id : gen `temp'=Total_Business_Assets[_n-1]  
			replace Total_Business_Assets= `temp'+Total_Business_Assets  if Survey_Version==3
	
		tempvar temp
			sort Id Business_Owner_Id Business_Name Survey_Version
			by Id : gen `temp'=Total_Business_Assets[_n-1]  
			replace Total_Business_Assets= `temp'+Total_Business_Assets  if Survey_Version==4
	

	************************************
	**LABOR WORKED BY OWNER
	************************************	
	gen Owner_Hours_Week=hhroster_15 if Survey_Version==1 & ( Business_Open==1)
	replace Owner_Hours_Week=Labor_Hours if Survey_Version==2
	replace Owner_Hours_Week=q16 if  Survey_Version==3|Survey_Version==4|Survey_Version==5
	recode Owner_Hours_Week (-555 -666 -999=.)

	gen Owner_Days_Month=hhroster_16 if Survey_Version==1& (Business_Open==1)
	replace Owner_Days_Month=Labor_Day if Survey_Version==2
	replace Owner_Days_Month=q17 if  Survey_Version==3|Survey_Version==4|Survey_Version==5
	recode Owner_Days_Month (-555 -666 -999=.)
	
	************************************
	**LABOR WORKED BY CLIENT
	************************************	
	gen Client_Hours_Week=hhroster_15 if Client==1
	replace Client_Hours_Week=q16 if Client_Hours_Week==. & Client==1
	replace Client_Hours_Week=Labor_Hours if Survey_Version==2 & Client==1
	recode Client_Hours_Week (-666 -999=.)

	gen Client_Days_Month=hhroster_16 if Client==1
	replace Client_Days_Month=q17 if Client_Days_Month==. & Client==1
	replace Client_Days_Month=Labor_Day if Survey_Version==2 & Client==1
	recode Client_Days_Month (-666 -999=.)
	
	************************************
	**LABOR WORKED BY OTHERS THAN OWNER
	************************************		
	recode Labor_1 Labor_2 Labor_3 Labor_4  Labor_5 Labor_6 Labor_7 Labor_8 Labor_9 Labor_10 (-666 -998 -999 -555=.)(-777=0)
	recode Labor_2 (.=0) if Labor_1==0
	recode Labor_4 Labor_5 (.=0) if Labor_3==0
	recode Labor_9 Labor_10 (.=0) if Labor_8==0
	recode Labor_7 (.=0) if Labor_6==0

	************************************
	**LABOUR HOURS
	************************************
	egen NonHH_Labor=rowtotal(Labor_6 Labor_8),m
		replace NonHH_Labor=l1 if Survey_Version==5
	gen NonHH_Labor_Dummy=NonHH_Labor!=0 if (Survey_Version==1| Survey_Version==2 | Survey_Version==5) & !missing(NonHH_Labor)
	egen NonHH_Labor_Hrs=rowtotal(Labor_7 Labor_9),m
		replace NonHH_Labor_Hrs=l2 if Survey_Version==5
		
	egen HH_Labor=rowtotal(Labor_1 Labor_3),m
	gen HH_Labor_Dummy=HH_Labor!=0 if (Survey_Version==1| Survey_Version==2 ) & !missing(HH_Labor)
	egen HH_Labor_Hrs=rowtotal(Labor_2 Labor_4) if (Survey_Version==1| Survey_Version==2) & !missing(HH_Labor)
	

	
		
***************************************************************************************************************
*
** STEP 5- CLIENT DEMOGRAPHIC CHARACTERISTICS
*
***************************************************************************************************************

	recode hhroster_2 (2=0)
	
	gen married_person=hhroster_5==1 

	local i=1
	local var1 Gender  Education Age Married  HH_Size
	local var2 gender  education age married hhsize
	foreach var of varlist hhroster_2  hhroster_8 hhroster_4 married_person hhid_8{
	
		local var3: word `i' of `var1'
		
		local var4: word `i' of `var2'
		
		gen `var4'=`var' if  client==1
		gen miss_`var4'=`var4'==.
		
		bys Id: egen `var3'_Followup=max(`var4')		
		gen miss_`var3'_Followup=`var3'_Followup==.
		
		drop `var4'
		
		local i=`i'+1
		}
	
	*** add the dummy "female"
	gen Female_Followup = Gender_Followup == 0
	
	************************************
	**CLIENT PROFITS
	************************************			
	gen Client_Profits_30Days=Profits_30Days if Client==1
		
	
	************************************
	**CLIENT CHARACTERISTICS AT HOUSEHOLD LEVEL
	************************************		
	recode Psychometric_* (-999 -666 -555 -998=.)
	
	foreach var in  Psychometric_1 Psychometric_2 Psychometric_3 Psychometric_4 Psychometric_5 Psychometric_6 ///
	Psychometric_7 Psychometric_8 Psychometric_9 Psychometric_10 Psychometric_11 Psychometric_12 ///
	Psychometric_13 Psychometric_14 Psychometric_15 Psychometric_16 Psychometric_17 ///
	TV_hhincome_1 child_0to5 child_6to12 ///
	Number_Fixed_Salary Number_Daily_Wage ///
	Number_Self_Employed Total_Nu_Bus HH_Assets Total_Assets  ///
	 digitspan_1 {
		bys Id: egen B_`var'=max(`var')
		
	}
	
	foreach v of varlist Psychometric_1 Psychometric_2 Psychometric_3 Psychometric_4 Psychometric_5 Psychometric_6 ///
	Psychometric_7 Psychometric_8 Psychometric_9 Psychometric_10 Psychometric_11 Psychometric_12 ///
	Psychometric_13 Psychometric_14 Psychometric_15 Psychometric_16 Psychometric_17 ///
	TV_hhincome_1 child_0to5 child_6to12 ///
	Number_Fixed_Salary Number_Daily_Wage ///
	Number_Self_Employed Total_Nu_Bus HH_Assets ///
	 digitspan_1{
	    local `v'_label: var label `v'
		label var B_`v' `"``v'_label'"'
		}

		
	recode Owner_Hist_1 Owner_Hist_2 Owner_Hist_3(-999 -998 -666=.)
	*business was closed
	recode Owner_Hist_1 (-777=.)
	*other primary business
	recode Owner_Hist_2 (-888=0)

	tempvar temp1 temp2 temp3
		gen `temp1'=Owner_Hist_1 if client==1
		bys Id: egen B_ChangeSales_2014=max(`temp1')
		gen `temp2'=(Owner_Hist_2==1) if client==1
		bys Id: egen B_Primary_Job_5yrs=max(`temp2')		
		gen `temp3'=(Owner_Hist_3) if client==1
		bys Id: egen B_Wage_Exit_SE=max(`temp3')	
		
	foreach var of varlist B_*{
		gen miss_`var'=`var'==.
	}
		
***********************************************************************************************************************************	
*
** STEP 6- MISSING VARIABLES
*
***********************************************************************************************************************************

	************************************
	**MISSING INCOME AND PROFITS
	************************************		
	foreach var in  Profits_30Days Income Client_Profits_30Days  ///
		Inventory Total_Business_Assets Owner_Hours_Week Owner_Days_Month ///
		HH_Labor HH_Labor_Hrs Labor_5 NonHH_Labor ///
		NonHH_Labor_Hrs Labor_10 NonHH_Labor_Dummy HH_Labor_Dummy {
		gen miss_`var'=`var'==.
		}

	
*******************************************************************************************************************************************************
*LABELING VARIABLES
*********************************************************************************************************************************************************

	
		
	label var survey_date "Survey Date"
	label var Income "HH income in past 30 days"
	label var Gender_Followup "Respodent is Male"
	label var Female_Followup "Respodent is Female"
	label var Education_Followup "Respodent's Education"
	label var Age_Followup "Respodent's Age"
	label var Married_Followup "Respodent is Married"
	label var HH_Size_Followup "Respodent's Household Size"
	label var Revenues_30Days  "Revenues in Past 30 Days"
	label var Profits_30Days  "Profits in Past 30 Days"
	label var Total_Business_Assets  "Total Business Assets"
	label var Owner_Hours_Week  "Hours Worked in the Previous 7 Days"
	label var Owner_Days_Month  "Days Worked in Previous 30 Days"
	label var NonHH_Labor  "Total Non-HH Workers"
	label var NonHH_Labor_Dummy  "Employs Non-HH Workers"
	label var NonHH_Labor_Hrs  "Total Hours Worked by Non-HH Workers"
	label var HH_Labor   "Total HH Workers"
	label var HH_Labor_Dummy  "Employs HH Workers"
	label var HH_Labor_Hrs  "Total Hours Worked by HH Workers"
	label var Winner  "Lottery Winner in Round"
	label var Winner2  "Ever won Lottery Winner"
	label var GroupNumber  "Ranking Group ID"
	label var Surveyor_Code  "Surveyor Code"	
	label var Survey_Version  "Survey Round"	
	label var survey_month "Survey Month"
	label var Client "Respondent"
	label var B_ChangeSales_2014 "Change in Sales since 2014"
	label var B_Wage_Exit_SE "Wage to Exit Self-Employment"
	label var Client_Profits_30Days "Client Profits in Past 30 Days"
	label var miss_Profits_30Days "Profits 30 Days Variable Missing"
	label var miss_Client_Profits_30Days "Client Profits 30 Days Variable Missing"
	label var B_Primary_Job_5yrs "Primary Job in 5 Yrs will be Self-Employment"
	label var B_manufacturing "Client Business Manufacturing"
	label var B_retail "Client Business Retail"
	label var B_service "Client Business Service"	
	label var B_agriculture "Client Business Agriculture"

	label var B_Total_Assets "Value of HH and business assets at baseline"
	label var B_digitspan_1 "Client's digitspan score"
*******************************************************************************************************************************************************
*LABELING DATA
*********************************************************************************************************************************************************
	
	order GroupNumber Id Business_Owner_Id Business_Name Survey_Version survey_date Business_Open Business_Open_Details Client LotteryWinner Public Revealed Incentives Surveyor_Code Final_Randomization_Cluster     Attriter  

	
	label data "ATTENTION: Type notes in the command line for information on this dataset"
	notes: do file that creates business level variables is called "variable creation business level"


****************************************
*SAVING
****************************************

	drop __*
	
	sort Id Business_Owner_Id Business_Name Survey_Version

	save "$data_final/Panel Business Level with Variables.dta", replace
